package com.touchportfolio.loadQueue.loaders
{
	import be.vandaag.loadQueue.IQueueLoader;
	
	import fl.video.FLVPlayback;
	import fl.video.VideoEvent;
	
	import flash.events.Event;

	public class VideoLoader extends FLVPlayback implements IQueueLoader
	{
		public static const VIDEO_FINISHED:String    = "video finished";
		public static const LOADED:String 			 = "loaded";
		
		private var _path:String;
		private var _loop:Boolean;
		private var _width:Number;
		private var _height:Number;
		
		public function VideoLoader(path:String, width:Number, height:Number ,loop:Boolean = false)
		{
			this.addEventListener(VideoEvent.COMPLETE, onVideoEnd);
			this.addEventListener(VideoEvent.READY, onVideoLoaded);
			_path   = path;
			_loop   = loop;
			_width  = width;
			_height = height
		}
		
		public function run():void
		{
			this.load(_path);
			
			this.setSize(_width, _height);
		}
		
		public override function play(arg0:String=null, arg1:Number=0, arg2:Boolean=false):void
		{
			super.play(arg0, arg1, arg2);
		}
		
		private function onVideoLoaded(e:VideoEvent):void
		{
			dispatchEvent(new Event(LOADED));
		}
		
		private function onVideoEnd(e:VideoEvent):void
		{
			if(_loop)
			{
				this.seekSeconds(0);
				if(!this.playing) this.play();	
			}
			else
			{
				dispatchEvent(new Event(VIDEO_FINISHED));
			}
			
		}	
	}
}